package zhongbo.javaweb.instagram.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11Nio2Protocol;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 说明：配置同时支持http及http端口访问
 * <p>
 * #link https://www.cnblogs.com/lianggp/p/8136540.html
 *
 * @author 钟波notebook
 * @since 2020年07月29日 星期三 19:02:50
 */
@Data
@ConfigurationProperties(prefix = "server.http")
@PropertySource(value = "classpath:application.yml", encoding = "utf-8")
@Configuration
@Slf4j
public class HttpConfig {

    /**
     * 附加http端口
     */
    private Integer port;

    /**
     * 是否启用附加http端口服务
     */
    private Boolean enabled;

    /*/**
    // * 适用于 spring boot 1.5.X以下版本
    // *
    // * @return tomcat嵌入式容器工厂
    // * /
    //import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;//old for BOOT 1.4.0.RELEASE
    //import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;//old for BOOT 1.4.0.RELEASE
    //@Bean
    //public EmbeddedServletContainerFactory servletContainer() {
    //    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    //    // 添加http
    //    if (enabled) {
    //        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
    //    }
    //    return tomcat;
    //}*/

    /**
     * 配置http
     * <p>
     * old for BOOT 1.4.0.RELEASE
     *
     * @return http 连接器
     */
    @Deprecated
    private Connector createStandardConnector() {
        Connector connector = new Connector(Http11Nio2Protocol.class.getName());
        connector.setPort(port);
        return connector;
    }


    /**
     * springboot2 写法
     * <p>
     * #link https://blog.csdn.net/l4642247/article/details/81631770
     *
     * @return Tomcat控制层Web服务器工厂
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    //@Bean//new for BOOT 2.2.1.RELEASE
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(port);
        // new for BOOT 2.2.1.RELEASE
        // 设置为false时,将会跳转到redirectPort;
        // @link https://blog.csdn.net/qq_19385067/article/details/117116322
        connector.setSecure(true);
        //监听到http的端口号后转向到的https的端口号
        //connector.setRedirectPort(httpsPort);
        return connector;
    }

}
